use actix_web::{web, App, HttpResponse, HttpServer,middleware};
use serde_json::json;
use actix_session::CookieSession;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let (host, port) = {
        let app_conf = neusoft_club_backend::global_config!(app);
        (app_conf.get_host(), app_conf.get_port())
    };

    neusoft_club_backend::db_init!().await;
    let server_future = HttpServer::new(move  || {
        let (cookie_name,cookie_private_key) = {
            let cookie_conf = neusoft_club_backend::global_config!(cookie);
            (cookie_conf.get_name(),cookie_conf.get_private_key())  
        };
        App::new()
            .wrap(middleware::NormalizePath::default())
            .wrap(CookieSession::signed(&cookie_private_key).name(cookie_name).http_only(true).lazy(true))
            .configure(neusoft_club_backend::controllers::config_controller)
            .default_service(web::to(|| {
                HttpResponse::NotFound().json(json!({"success":0,"code":404}))
            }))
    })
    .bind(format!("{}:{}", host, port))
    .unwrap()
    .run();
    println!("server listening http://{}:{}", host, port);
    server_future.await
}
